﻿using Sustainalytics.ClientDW.Entities;
using Sustainalytics.ClientDW.Entities.Alerts;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Utils;
//using Sustainalytics.Utils.EventSource;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.Alerts.AlertsEngine
{
    public class AlertsManager : IAlertsManager
    {
        private readonly Dictionary<Guid, CompanyAlert> _alertsDict;
        private readonly IRepositoryCollection<Alert> _alertRepositoryCollection;
        private readonly IRepositoryCollection<CorporateData> _corporateDataRepositoryCollection;

        public const int Semimonthly = 1;

        public AlertsManager(Dictionary<Guid, CompanyAlert> alertsDict, IRepositoryCollection<Alert> alertRepositoryCollection, IRepositoryCollection<CorporateData> corporateDataRepositoryCollection)
        {
            _alertsDict = alertsDict;
            _alertRepositoryCollection = alertRepositoryCollection;
            _corporateDataRepositoryCollection = corporateDataRepositoryCollection;

        }

        public IList<Alert> GetAlertsFromPeriod(DateTime? beginCreatedDate, DateTime? endCreatedDate, string connectionStringClientDw, Guid sessionId)
        {

            return _alertRepositoryCollection.ReadAllWhere(a =>
                (beginCreatedDate.HasValue && a.Created >= beginCreatedDate.Value)
                && (endCreatedDate.HasValue && a.Created <= endCreatedDate.Value))
                    .Where(a =>
                    {
                        CompanyAlert companyAlert;
                        if (!_alertsDict.ContainsKey(a.CompanyId))
                        {
                            var corporateData = _corporateDataRepositoryCollection.Read(a.CompanyId);
                            if (corporateData == null)
                            {
                                AlertsEngineSerilog.Log.AlertWithoutCompanyInCorporateData(sessionId);
                                return false;
                            }

                            companyAlert = new CompanyAlert()
                            {
                                Name = corporateData.CompanyName,
                                UrlFriendlyName = corporateData.CompanyNameUrlFriendly,
                                Country = corporateData.Country,
                                PeerGroup = corporateData.PeerGroup,
                                CIQID = corporateData.CapitalIqId.ToString(),
                                Ticker = corporateData.MainTicker ?? string.Empty,
                                ExchangeCode = corporateData.MainExchange ?? string.Empty
                            };
                            _alertsDict.Add(a.CompanyId, companyAlert);
                        }
                        else
                        {
                            companyAlert = _alertsDict[a.CompanyId];
                        }

                        a.CompanyAlert = companyAlert;
                        return true;
                    })
                .ToList();
        }

        public void CleanUp()
        {
            _alertsDict.Clear();
        }
    }
}
